Follow-Me Project

Congratulations on reaching the final project of the Robotics Nanodegree!

Previously, you worked on the Semantic Segmentation lab where you built a deep learning network that locates a particular human target within an image. For this project, you will utilize what you implemented and learned from that lab and extend it to train a deep learning model that will allow a simulated quadcopter to follow around the person that it detects!

Most of the code below is similar to the lab with some minor modifications. You can start with your existing solution, and modify and improve upon it to train the best possible model for this task.

You can click on any of the following to quickly jump to that part of this notebook:

  1. Data Collection
  2. FCN Layers
  3. Build the Model
  4. Training
  5. Prediction
  6. Evaluation

Data Collection

We have provided you with a starting dataset for this project. Download instructions can be found in the README for this project's repo. Alternatively, you can collect additional data of your own to improve your model. Check out the "Collecting Data" section in the Project Lesson in the Classroom for more details!

In [1]:
import os
import glob
import sys
import tensorflow as tf

from scipy import misc
import numpy as np

from tensorflow import image

from utils import scoring_utils

# For Tensorflow 1.2.1:
# from tensorflow.contrib.keras.python import keras
# from tensorflow.contrib.keras.python.keras import layers, models
# from utils.separable_conv2d import SeparableConv2DKeras, BilinearUpSampling2D
# For Tensorflow 1.4.0 + Keras 2.0.8:
import keras
from keras import layers, models
from keras.layers import SeparableConv2D as SeparableConv2DKeras
from keras.layers import UpSampling2D as BilinearUpSampling2D

from utils import data_iterator
from utils import plotting_tools 
from utils import model_tools
Using TensorFlow backend.

FCN Layers

In the Classroom, we discussed the different layers that constitute a fully convolutional network (FCN). The following code will introduce you to the functions that you need to build your semantic segmentation model.

Separable Convolutions

The Encoder for your FCN will essentially require separable convolution layers, due to their advantages as explained in the classroom. The 1x1 convolution layer in the FCN, however, is a regular convolution. Implementations for both are provided below for your use. Each includes batch normalization with the ReLU activation function applied to the layers.

In [2]:
def separable_conv2d_batchnorm(input_layer, filters, strides=1):
    output_layer = SeparableConv2DKeras(filters=filters,kernel_size=3, strides=strides,
                             padding='same', activation='relu')(input_layer)
    
    output_layer = layers.BatchNormalization()(output_layer) 
    return output_layer

def conv2d_batchnorm(input_layer, filters, kernel_size=3, strides=1):
    output_layer = layers.Conv2D(filters=filters, kernel_size=kernel_size, strides=strides, 
                      padding='same', activation='relu')(input_layer)
    
    output_layer = layers.BatchNormalization()(output_layer) 
    return output_layer

Bilinear Upsampling

The following helper function implements the bilinear upsampling layer. Upsampling by a factor of 2 is generally recommended, but you can try out different factors as well. Upsampling is used in the decoder block of the FCN.

In [3]:
def bilinear_upsample(input_layer):
    output_layer = BilinearUpSampling2D((2,2))(input_layer)
    return output_layer

Build the Model

In the following cells, you will build an FCN to train a model to detect and locate the hero target within an image. The steps are:

  • Create an encoder_block
  • Create a decoder_block
  • Build the FCN consisting of encoder block(s), a 1x1 convolution, and decoder block(s). This step requires experimentation with different numbers of layers and filter sizes to build your model.

Encoder Block

Create an encoder block that includes a separable convolution layer using the separable_conv2d_batchnorm() function. The filters parameter defines the size or depth of the output layer. For example, 32 or 64.

In [4]:
def encoder_block(input_layer, filters, strides):
    
    # TODO Create a separable convolution layer using the separable_conv2d_batchnorm() function.
    output_layer = separable_conv2d_batchnorm(input_layer, filters, strides=strides)
    
    return output_layer

Decoder Block

The decoder block is comprised of three parts:

  • A bilinear upsampling layer using the upsample_bilinear() function. The current recommended factor for upsampling is set to 2.
  • A layer concatenation step. This step is similar to skip connections. You will concatenate the upsampled small_ip_layer and the large_ip_layer.
  • Some (one or two) additional separable convolution layers to extract some more spatial information from prior layers.
In [5]:
def decoder_block(small_ip_layer, large_ip_layer, filters):
    # TODO Upsample the small input layer using the bilinear_upsample() function.
    up = bilinear_upsample(small_ip_layer)
    
    # TODO Concatenate the upsampled and large input layers using layers.concatenate
    output_layer = layers.concatenate([up, large_ip_layer])
    
    # TODO Add some number of separable convolution layers
    output_layer = separable_conv2d_batchnorm(output_layer, filters)
    output_layer = separable_conv2d_batchnorm(output_layer, filters)
    
    return output_layer

Model

Now that you have the encoder and decoder blocks ready, go ahead and build your FCN architecture!

There are three steps:

  • Add encoder blocks to build the encoder layers. This is similar to how you added regular convolutional layers in your CNN lab.
  • Add a 1x1 Convolution layer using the conv2d_batchnorm() function. Remember that 1x1 Convolutions require a kernel and stride of 1.
  • Add decoder blocks for the decoder layers.
In [6]:
def fcn_model(inputs, num_classes):
    
    # TODO Add Encoder Blocks. 
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.
    conv1 = encoder_block(inputs, 64, 2)
    conv2 = encoder_block(conv1, 128, 2)
    conv3 = encoder_block(conv2, 128, 2)

    # TODO Add 1x1 Convolution layer using conv2d_batchnorm().
    one_on_one = conv2d_batchnorm(conv3, 64, kernel_size=1, strides=1)
    
    # TODO: Add the same number of Decoder Blocks as the number of Encoder Blocks
    up1 = decoder_block(one_on_one, conv2, 128)
    up2 = decoder_block(up1, conv1, 128)
    up3 = bilinear_upsample(up2)
    
    x = up3
    
    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 3, activation='softmax', padding='same')(x)

Training

The following cells will use the FCN you created and define an ouput layer based on the size of the processed image and the number of classes recognized. You will define the hyperparameters to compile and train your model.

Please Note: For this project, the helper code in data_iterator.py will resize the copter images to 160x160x3 to speed up training.

In [7]:
"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""

image_hw = 160
image_shape = (image_hw, image_hw, 3)
inputs = layers.Input(image_shape)
num_classes = 3

# Call fcn_model()
output_layer = fcn_model(inputs, num_classes)

Hyperparameters

Define and tune your hyperparameters.

  • batch_size: number of training samples/images that get propagated through the network in a single pass.
  • num_epochs: number of times the entire training dataset gets propagated through the network.
  • steps_per_epoch: number of batches of training images that go through the network in 1 epoch. We have provided you with a default value. One recommended value to try would be based on the total number of images in training dataset divided by the batch_size.
  • validation_steps: number of batches of validation images that go through the network in 1 epoch. This is similar to steps_per_epoch, except validation_steps is for the validation dataset. We have provided you with a default value for this as well.
  • workers: maximum number of processes to spin up. This can affect your training speed and is dependent on your hardware. We have provided a recommended value to work with.
In [8]:
learning_rate = 0.001
batch_size = 16
num_epochs = 20
steps_per_epoch = 350
validation_steps = 50
workers = 16
In [9]:
"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
# Define the Keras model and compile it for training
model = models.Model(inputs=inputs, outputs=output_layer)

model.compile(optimizer=keras.optimizers.Adam(learning_rate), loss='categorical_crossentropy')

# Data iterators for loading the training and validation data
train_iter = data_iterator.BatchIteratorSimple(batch_size=batch_size,
                                               data_folder=os.path.join('..', 'data', 'train'),
                                               image_shape=image_shape,
                                               shift_aug=True)

val_iter = data_iterator.BatchIteratorSimple(batch_size=batch_size,
                                             data_folder=os.path.join('..', 'data', 'validation'),
                                             image_shape=image_shape)

logger_cb = plotting_tools.LoggerPlotter()
es = keras.callbacks.EarlyStopping(monitor='val_loss',
                                   min_delta=0,
                                   patience=1,
                                   verbose=0, mode='auto')
callbacks = [logger_cb]

model.fit_generator(train_iter,
                    steps_per_epoch = steps_per_epoch, # the number of batches per epoch,
                    epochs = num_epochs, # the number of epochs to train for,
                    validation_data = val_iter, # validation iterator
                    validation_steps = validation_steps, # the number of batches to validate on
                    callbacks=callbacks,
                    workers = workers)
Epoch 1/20
349/350 [============================>.] - ETA: 0s - loss: 0.1709- ETA: 1s -
350/350 [==============================] - 42s - loss: 0.1707 - val_loss: 0.1260
Epoch 2/20
349/350 [============================>.] - ETA: 0s - loss: 0.0455
350/350 [==============================] - 34s - loss: 0.0455 - val_loss: 0.0450
Epoch 3/20
349/350 [============================>.] - ETA: 0s - loss: 0.0426
350/350 [==============================] - 36s - loss: 0.0425 - val_loss: 0.0367
Epoch 4/20
349/350 [============================>.] - ETA: 0s - loss: 0.0386- ETA: 0s - loss:
350/350 [==============================] - 34s - loss: 0.0386 - val_loss: 0.0663
Epoch 5/20
349/350 [============================>.] - ETA: 0s - loss: 0.0376
350/350 [==============================] - 34s - loss: 0.0376 - val_loss: 0.0802
Epoch 6/20
349/350 [============================>.] - ETA: 0s - loss: 0.0336
350/350 [==============================] - 36s - loss: 0.0336 - val_loss: 0.0331
Epoch 7/20
349/350 [============================>.] - ETA: 0s - loss: 0.0355
350/350 [==============================] - 34s - loss: 0.0355 - val_loss: 0.0397
Epoch 8/20
349/350 [============================>.] - ETA: 0s - loss: 0.0337
350/350 [==============================] - 34s - loss: 0.0336 - val_loss: 0.0278
Epoch 9/20
349/350 [============================>.] - ETA: 0s - loss: 0.0324- ETA
350/350 [==============================] - 34s - loss: 0.0323 - val_loss: 0.0367
Epoch 10/20
349/350 [============================>.] - ETA: 0s - loss: 0.0313
350/350 [==============================] - 34s - loss: 0.0312 - val_loss: 0.0329
Epoch 11/20
349/350 [============================>.] - ETA: 0s - loss: 0.0302
350/350 [==============================] - 34s - loss: 0.0303 - val_loss: 0.0335
Epoch 12/20
349/350 [============================>.] - ETA: 0s - loss: 0.0302
350/350 [==============================] - 34s - loss: 0.0302 - val_loss: 0.0286
Epoch 13/20
349/350 [============================>.] - ETA: 0s - loss: 0.0296- ETA: 0s - loss: 0.0
350/350 [==============================] - 34s - loss: 0.0296 - val_loss: 0.0423
Epoch 14/20
349/350 [============================>.] - ETA: 0s - loss: 0.0281- ETA: 0s - loss: 0.028
350/350 [==============================] - 34s - loss: 0.0281 - val_loss: 0.0350
Epoch 15/20
349/350 [============================>.] - ETA: 0s - loss: 0.0270
350/350 [==============================] - 36s - loss: 0.0270 - val_loss: 0.0362
Epoch 16/20
349/350 [============================>.] - ETA: 0s - loss: 0.0277
350/350 [==============================] - 36s - loss: 0.0276 - val_loss: 0.0326
Epoch 17/20
349/350 [============================>.] - ETA: 0s - loss: 0.0260
350/350 [==============================] - 35s - loss: 0.0259 - val_loss: 0.0272
Epoch 18/20
349/350 [============================>.] - ETA: 0s - loss: 0.0269
350/350 [==============================] - 34s - loss: 0.0269 - val_loss: 0.0323
Epoch 19/20
349/350 [============================>.] - ETA: 0s - loss: 0.0252
350/350 [==============================] - 34s - loss: 0.0252 - val_loss: 0.0325
Epoch 20/20
349/350 [============================>.] - ETA: 0s - loss: 0.0243
350/350 [==============================] - 34s - loss: 0.0244 - val_loss: 0.0247
Out[9]:
<keras.callbacks.History at 0x7f722abf7eb8>
In [10]:
# Save your trained model weights
weight_file_name = 'model_weights'
model_tools.save_network(model, weight_file_name)

Prediction

Now that you have your model trained and saved, you can make predictions on your validation dataset. These predictions can be compared to the mask images, which are the ground truth labels, to evaluate how well your model is doing under different conditions.

There are three different predictions available from the helper code provided:

  • patrol_with_targ: Test how well the network can detect the hero from a distance.
  • patrol_non_targ: Test how often the network makes a mistake and identifies the wrong person as the target.
  • following_images: Test how well the network can identify the target while following them.
In [11]:
# If you need to load a model which you previously trained you can uncomment the codeline that calls the function below.

# weight_file_name = 'model_weights'
# restored_model = model_tools.load_network(weight_file_name)
In [12]:
model.summary()
____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_1 (InputLayer)             (None, 160, 160, 3)   0                                            
____________________________________________________________________________________________________
separable_conv2d_1 (SeparableCon (None, 80, 80, 64)    283         input_1[0][0]                    
____________________________________________________________________________________________________
batch_normalization_1 (BatchNorm (None, 80, 80, 64)    256         separable_conv2d_1[0][0]         
____________________________________________________________________________________________________
separable_conv2d_2 (SeparableCon (None, 40, 40, 128)   8896        batch_normalization_1[0][0]      
____________________________________________________________________________________________________
batch_normalization_2 (BatchNorm (None, 40, 40, 128)   512         separable_conv2d_2[0][0]         
____________________________________________________________________________________________________
separable_conv2d_3 (SeparableCon (None, 20, 20, 128)   17664       batch_normalization_2[0][0]      
____________________________________________________________________________________________________
batch_normalization_3 (BatchNorm (None, 20, 20, 128)   512         separable_conv2d_3[0][0]         
____________________________________________________________________________________________________
conv2d_1 (Conv2D)                (None, 20, 20, 64)    8256        batch_normalization_3[0][0]      
____________________________________________________________________________________________________
batch_normalization_4 (BatchNorm (None, 20, 20, 64)    256         conv2d_1[0][0]                   
____________________________________________________________________________________________________
up_sampling2d_1 (UpSampling2D)   (None, 40, 40, 64)    0           batch_normalization_4[0][0]      
____________________________________________________________________________________________________
concatenate_1 (Concatenate)      (None, 40, 40, 192)   0           up_sampling2d_1[0][0]            
                                                                   batch_normalization_2[0][0]      
____________________________________________________________________________________________________
separable_conv2d_4 (SeparableCon (None, 40, 40, 128)   26432       concatenate_1[0][0]              
____________________________________________________________________________________________________
batch_normalization_5 (BatchNorm (None, 40, 40, 128)   512         separable_conv2d_4[0][0]         
____________________________________________________________________________________________________
separable_conv2d_5 (SeparableCon (None, 40, 40, 128)   17664       batch_normalization_5[0][0]      
____________________________________________________________________________________________________
batch_normalization_6 (BatchNorm (None, 40, 40, 128)   512         separable_conv2d_5[0][0]         
____________________________________________________________________________________________________
up_sampling2d_2 (UpSampling2D)   (None, 80, 80, 128)   0           batch_normalization_6[0][0]      
____________________________________________________________________________________________________
concatenate_2 (Concatenate)      (None, 80, 80, 192)   0           up_sampling2d_2[0][0]            
                                                                   batch_normalization_1[0][0]      
____________________________________________________________________________________________________
separable_conv2d_6 (SeparableCon (None, 80, 80, 128)   26432       concatenate_2[0][0]              
____________________________________________________________________________________________________
batch_normalization_7 (BatchNorm (None, 80, 80, 128)   512         separable_conv2d_6[0][0]         
____________________________________________________________________________________________________
separable_conv2d_7 (SeparableCon (None, 80, 80, 128)   17664       batch_normalization_7[0][0]      
____________________________________________________________________________________________________
batch_normalization_8 (BatchNorm (None, 80, 80, 128)   512         separable_conv2d_7[0][0]         
____________________________________________________________________________________________________
up_sampling2d_3 (UpSampling2D)   (None, 160, 160, 128) 0           batch_normalization_8[0][0]      
____________________________________________________________________________________________________
conv2d_2 (Conv2D)                (None, 160, 160, 3)   3459        up_sampling2d_3[0][0]            
====================================================================================================
Total params: 130,334
Trainable params: 128,542
Non-trainable params: 1,792
____________________________________________________________________________________________________

The following cell will write predictions to files and return paths to the appropriate directories. The run_num parameter is used to define or group all the data for a particular model run. You can change it for different runs. For example, 'run_1', 'run_2' etc.

In [13]:
run_num = 'run_2'

val_with_targ, pred_with_targ = model_tools.write_predictions_grade_set(model,
                                        run_num,'patrol_with_targ', 'sample_evaluation_data') 

val_no_targ, pred_no_targ = model_tools.write_predictions_grade_set(model, 
                                        run_num,'patrol_non_targ', 'sample_evaluation_data') 

val_following, pred_following = model_tools.write_predictions_grade_set(model,
                                        run_num,'following_images', 'sample_evaluation_data')

Now lets look at your predictions, and compare them to the ground truth labels and original images. Run each of the following cells to visualize some sample images from the predictions in the validation set.

In [14]:
# images while following the target
num_files = 3
im_files = plotting_tools.get_im_file_sample('sample_evaluation_data','following_images', run_num,
                                             n_file_names=num_files) 
for i in range(num_files):
    im_tuple = plotting_tools.load_images(im_files[i])
    plotting_tools.show_images(im_tuple)
    
In [15]:
# images while at patrol without target
num_files = 20
# im_files = plotting_tools.get_im_file_sample('sample_evaluation_data','patrol_non_targ', run_num,
#                                             n_file_names=num_files) 
im_files = plotting_tools.get_im_file_sample('sample_evaluation_data','patrol_non_targ', run_num,
                                            n_file_names=num_files) 
for i in range(num_files):
    im_tuple = plotting_tools.load_images(im_files[i])
    plotting_tools.show_images(im_tuple)
 
In [16]:
# images while at patrol with target
num_files = 100
im_files = plotting_tools.get_im_file_sample('sample_evaluation_data','patrol_with_targ', run_num,
                                             n_file_names=num_files) 
for i in range(num_files):
    im_tuple = plotting_tools.load_images(im_files[i])
    plotting_tools.show_images(im_tuple)
In [17]:
len(im_files)
Out[17]:
100

Evaluation

Evaluate your model! The following cells include several different scores to help you evaluate your model under the different conditions discussed during the Prediction step.

In [18]:
# Scores for while the quad is following behind the target. 
true_pos1, false_pos1, false_neg1, iou1 = scoring_utils.score_run_iou(val_following, pred_following)
number of validation samples intersection over the union evaulated on 542
average intersection over union for background is 0.9947431417704209
average intersection over union for other people is 0.2709456195817337
average intersection over union for the hero is 0.8561012886315188
number true positives: 539, number false positives: 0, number false negatives: 0
In [19]:
# Scores for images while the quad is on patrol and the target is not visable
true_pos2, false_pos2, false_neg2, iou2 = scoring_utils.score_run_iou(val_no_targ, pred_no_targ)
number of validation samples intersection over the union evaulated on 270
average intersection over union for background is 0.9831788579582973
average intersection over union for other people is 0.6366796888871262
average intersection over union for the hero is 0.0
number true positives: 0, number false positives: 24, number false negatives: 0
In [20]:
# This score measures how well the neural network can detect the target from far away
true_pos3, false_pos3, false_neg3, iou3 = scoring_utils.score_run_iou(val_with_targ, pred_with_targ)
number of validation samples intersection over the union evaulated on 322
average intersection over union for background is 0.9957875707175405
average intersection over union for other people is 0.3582449954066492
average intersection over union for the hero is 0.19162264867873302
number true positives: 123, number false positives: 0, number false negatives: 178
In [21]:
# Sum all the true positives, etc from the three datasets to get a weight for the score
true_pos = true_pos1 + true_pos2 + true_pos3
false_pos = false_pos1 + false_pos2 + false_pos3
false_neg = false_neg1 + false_neg2 + false_neg3

weight = true_pos/(true_pos+false_neg+false_pos)
print(weight)
0.7662037037037037
In [22]:
# The IoU for the dataset that never includes the hero is excluded from grading
final_IoU = (iou1 + iou3)/2
print(final_IoU)
0.523861968655
In [23]:
# And the final grade score is 
final_score = final_IoU * weight
print(final_score)
0.401384980613